﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Oracle.DataAccess.Client;

namespace GestionCegep.Objects
{
    public static class LogActions
    {
        public static string Debut = "Debut";
        public static string Confirmer = "Confirmer";
        public static string Annuler = "Annuler";
        public static string Defaire = "Defaire";
        public static string Refaire = "Refaire";
    }

    public class MyOracleTransaction
    {
        private int idTransaction;
        private OracleTransaction oracleTransaction;
        private OracleConnection oracleConnection;

        // Gestion des ID de transaction pour la journalisation
        public MyOracleTransaction(OracleTransaction oracleTransaction, OracleConnection oracleConnection)
        {
            OracleCommand command = oracleConnection.CreateCommand(); // Creation de la commande Oracle
            command.CommandText = "SELECT SEQ_TRANSACTION_ID.NEXTVAL FROM DUAL"; // On obtient le prochain numero de sequence pour les transactions a la base de donnees
            this.idTransaction = Convert.ToInt32(command.ExecuteScalar().ToString()); // On obtient le ID de transaction
            this.oracleTransaction = oracleTransaction;
            this.oracleConnection = oracleConnection;
        }

        public int IdTransaction
        {
            get
            {
                return idTransaction;
            }
        }

        public OracleTransaction OracleTransaction
        {
            get
            {
                return oracleTransaction;
            }
        }

        public OracleConnection Connection
        {
            get
            {
                return oracleTransaction.Connection;
            }
        }

        // Confirmation d'une transaction
        public void Commit()
        {
            oracleTransaction.Commit();
        }
        
        // Annulation d'une transaction
        public void Rollback()
        {
            oracleTransaction.Rollback();
        }

        // Journalisation des transactions
        public void PushLogToBD(string action, string query)
        {
            OracleCommand command = oracleConnection.CreateCommand(); // Creation de la commande Oracle
            query = query.Replace("'", "''");
            command.CommandText = String.Format("INSERT INTO JOURNAL(ID_TRANSACTION, ACTION, REQUETE) VALUES({0}, '{1}', '{2}')", idTransaction, action, query);
            command.ExecuteNonQuery(); // Ajout dans la table de journalisation (JOURNAL)
        }
    }
}
